package com.woniuxy.table.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.reservation.client.result.TakeNumClientDto;
import com.woniuxy.reservation.client.takenumclient.TakeNumQueryClient;
import com.woniuxy.table.common.TableResultCode;
import com.woniuxy.table.infra.mapper.DinnerTableMapper;
import com.woniuxy.table.infra.mapper.DinnerTableTypeMapper;
import com.woniuxy.table.infra.model.DinnerTable;
import com.woniuxy.table.infra.model.DinnerTableType;
import com.woniuxy.table.service.DinnerTableTypeService;
import com.woniuxy.table.service.param.AddTableTypeServiceParam;
import com.woniuxy.table.service.param.ModifyTableTypeServiceParam;
import exception.MallException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import result.CommonResultCodeEnum;
import result.Result;

import java.util.List;

/**
 * <p>
 * 餐桌类型表 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2023-07-20
 */
@Service
public class DinnerTableTypeServiceImpl extends ServiceImpl<DinnerTableTypeMapper, DinnerTableType> implements DinnerTableTypeService {
    @Autowired
    private DinnerTableTypeMapper dinnerTableTypeMapper;
    @Autowired
    private DinnerTableMapper dinnerTableMapper;
    @Autowired
    private TakeNumQueryClient takeNumQueryClient;
    @Override
    public void addTableType(AddTableTypeServiceParam addTableTypeServiceParam) {
        if (addTableTypeServiceParam.getMinSite()>addTableTypeServiceParam.getMaxSite()){
            throw new MallException(TableResultCode.DATA_ERROR);
        }
        QueryWrapper<DinnerTableType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("table_type_name",addTableTypeServiceParam.getTableTypeName()).eq("store_id",addTableTypeServiceParam.getStoreId());
        DinnerTableType dinnerTableType = dinnerTableTypeMapper.selectOne(queryWrapper);
        if (dinnerTableType!=null){
            throw new MallException(TableResultCode.DUPLICATE_TYPE);
        }
        DinnerTableType newDinnerTableType = new DinnerTableType();
        newDinnerTableType.setTableTypeName(addTableTypeServiceParam.getTableTypeName());
        newDinnerTableType.setMinSite(addTableTypeServiceParam.getMinSite());
        newDinnerTableType.setMaxSite(addTableTypeServiceParam.getMaxSite());
        newDinnerTableType.setStoreId(addTableTypeServiceParam.getStoreId());
        save(newDinnerTableType);
    }

    @Override
    public void modifyTableType(ModifyTableTypeServiceParam modifyTableTypeServiceParam) {
        DinnerTableType dinnerTableType = getById(modifyTableTypeServiceParam.getTableTypeId());
        if (dinnerTableType==null){
            throw new MallException(TableResultCode.TYPE_DOES_NOT_EXIST);
        }
        if (modifyTableTypeServiceParam.getMinSite()>modifyTableTypeServiceParam.getMaxSite()){
            throw new MallException(TableResultCode.DATA_ERROR);
        }
        if (dinnerTableType.getStoreId()!= modifyTableTypeServiceParam.getStoreId()){
            throw new MallException(TableResultCode.DATA_ERROR);
        }
        if (modifyTableTypeServiceParam.getTableTypeName()==null||modifyTableTypeServiceParam.getTableTypeName().equals("")){
            throw new MallException(TableResultCode.DATA_ERROR);
        }
        DinnerTableType dinnerTableType1 = BeanUtil.toBean(modifyTableTypeServiceParam, DinnerTableType.class);

        updateById(dinnerTableType1);
    }

    @Override
    public void deleteTableType(Integer storeId, Integer tableTypeId) {
        DinnerTableType dinnerTableType = getById(tableTypeId);
        if (dinnerTableType==null){
            throw new MallException(TableResultCode.TYPE_DOES_NOT_EXIST);
        }
        if (dinnerTableType.getStoreId()!=storeId){
            throw new MallException(TableResultCode.DATA_ERROR);
        }
        Result<List<TakeNumClientDto>> listResult = takeNumQueryClient.searchTakeNumByTableTypeId(storeId, tableTypeId);
        if (listResult.getData().size()!=0) {
            throw new MallException(TableResultCode.DATA_ERROR);
        }
        QueryWrapper<DinnerTable> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("table_type_id",dinnerTableType.getTableTypeId());
        List<DinnerTable> dinnerTables = dinnerTableMapper.selectList(queryWrapper);
        if (dinnerTables!=null||dinnerTables.size()!=0){
            throw new MallException(TableResultCode.DELETE_FAILED);
        }
        removeById(dinnerTableType.getTableTypeId());
    }

    @Override
    public DinnerTableType getTableType(Integer storeId, Integer tableTypeId) {
        DinnerTableType dinnerTableType = getById(tableTypeId);
        if (dinnerTableType.getStoreId()!=storeId) {
            throw new MallException("当前餐桌类型不存在", CommonResultCodeEnum.INVALID_PARAM);
        }
        return dinnerTableType;
    }

    @Override
    public List<DinnerTableType> getAllTableType(Integer storeId) {
        QueryWrapper<DinnerTableType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("store_id", storeId);
        List<DinnerTableType> dinnerTableTypes = dinnerTableTypeMapper.selectList(queryWrapper);
        return dinnerTableTypes;
    }
}
